<template>
  <div>
    <el-form
      ref="editUploadForm"
      :model="editUploadForm"
      label-width="120px"
      :rules="rules"
      class="upload-form"
    >
      <h1 class="title">{{ title }}</h1>
      <el-form-item label="是否涉及">
        <el-checkbox
          v-model="editUploadForm.involve"
          @change="changeCheckbox"
          :disabled="isDisabled"
          >不涉及</el-checkbox
        >
      </el-form-item>
      <el-form-item
        label="预计完成时间"
        :prop="editUploadForm.involve ? '' : 'yjfinishTime'"
      >
        <el-date-picker
          v-model="editUploadForm.yjfinishTime"
          type="month"
          placeholder="请选预计完成时间"
          value-format="yyyy-MM"
          :disabled="editUploadForm.involve || isDisabled || isSave"
        >
        </el-date-picker>
      </el-form-item>
      <el-form-item
        label="完成时间"
        :prop="editUploadForm.involve ? '' : 'finishTime'"
      >
        <el-date-picker
          v-model="editUploadForm.finishTime"
          type="date"
          placeholder="请选完成时间"
          value-format="yyyy-MM-dd"
          :disabled="editUploadForm.involve || isDisabled"
        >
        </el-date-picker>
      </el-form-item>
      <el-form
        ref="uploadForm"
        :model="uploadForm"
        label-width="120px"
        :rules="rules"
      >
        <el-form-item label="申报资料" prop="shenBaoZiLiao">
          <el-row>
            <el-col :span="3">
              <el-checkbox
                v-model="editUploadForm.shenbao"
                @change="changeCheckbox1"
                :disabled="isDisabled || editUploadForm.involve"
                >不涉及</el-checkbox
              >
            </el-col>
            <el-col :span="21">
              <project-upload
                :upload-address="uploadForm.uploadAddress1"
                :type="1"
                ref="shenBaoZiLiao"
                :project-id="uploadProjectId"
                :auth-disabled="
                  editUploadForm.involve || isDisabled || editUploadForm.shenbao
                "
              ></project-upload>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="批复文件" prop="piFuWenJian">
          <el-row>
            <el-col :span="3">
              <el-checkbox
                v-model="editUploadForm.pifu"
                @change="changeCheckbox2"
                :disabled="isDisabled || editUploadForm.involve"
                >不涉及</el-checkbox
              >
            </el-col>
            <el-col :span="21">
              <project-upload
                :upload-address="uploadForm.uploadAddress2"
                :type="1"
                ref="piFuWenJian"
                :project-id="uploadProjectId"
                :auth-disabled="
                  editUploadForm.involve || isDisabled || editUploadForm.pifu
                "
              ></project-upload>
            </el-col>
          </el-row>
        </el-form-item>
        <el-form-item label="合同" prop="heTong" v-if="isShowHetong">
          <el-row>
            <el-col :span="3">
              <el-checkbox
                v-model="editUploadForm.hetong"
                @change="changeCheckbox3"
                :disabled="isDisabled || editUploadForm.involve"
                >不涉及</el-checkbox
              >
            </el-col>
            <el-col :span="21">
              <project-upload
                :upload-address="uploadForm.uploadAddress3"
                :type="1"
                ref="heTong"
                :project-id="uploadProjectId"
                :auth-disabled="
                  editUploadForm.involve || isDisabled || editUploadForm.hetong
                "
              ></project-upload>
            </el-col>
          </el-row>
        </el-form-item>
      </el-form>
      <el-form-item class="save-item">
        <div class="save-btn">
          <el-button
            v-if="isDisabled"
            type="primary"
            size="small"
            icon="el-icon-download"
            @click="downloadAllFile"
            >批量下载</el-button
          >
          <el-button
            type="primary"
            size="small"
            icon="el-icon-folder-checked"
            @click="save()"
            :disabled="isDisabled"
            >保 存</el-button
          >
        </div>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
import ProjectUpload from "@/views/archives/components/ProjectUpload";
import { updateDemand, getUpdateInfo } from "@/api/archives/project";
export default {
  name: "RuoyiUiProjecteditform",
  components: {
    ProjectUpload,
  },
  props: {
    openEditForm: {
      type: Boolean,
      default: false,
    },
    projectId: {
      type: Number,
      default: 0,
    },
    stageId: {
      type: Number,
      default: 0,
    },
    stageName: {
      type: String,
      default: "",
    },
    isDisabled: {
      type: Boolean,
      default: false,
    },
    title: {
      type: String,
      default: "",
    },
    isShowHetong: {
      type: Boolean,
      default: true,
    },
  },
  watch: {},
  data() {
    return {
      editUploadForm: {
        establishDemandId: null,
        stageId: null,
        yjfinishTime: null,
        finishTime: null,
        involve: false,
        stageName: null,
        shenbao: false,
        pifu: false,
        hetong: false,
      },
      rules: {
        // yjfinishTime: [
        //   { required: true, message: "预计完成时间不能为空", trigger: "blur" },
        // ],
        // finishTime: [
        //   { required: true, message: "完成时间不能为空", trigger: "blur" },
        // ],
      },
      uploadForm: {
        uploadAddress1: "",
        uploadAddress2: "",
        uploadAddress3: "",
      },
      uploadProjectId: 0,
      awaitDeledtFileList1: [],
      awaitDeledtFileList2: [],
      awaitDeledtFileList3: [],
      isSave: false,
    };
  },

  mounted() {},

  methods: {
    changeCheckbox(value) {
      // console.log(value);
      this.editUploadForm.shenbao = value;
      this.editUploadForm.pifu = value;
      this.editUploadForm.hetong = value;
      if (value) {
        this.editUploadForm.yjfinishTime = null;
        this.editUploadForm.finishTime = null;
        this.awaitDeledtFileList1 = this.$refs.shenBaoZiLiao.saveUploadFileList;
        this.awaitDeledtFileList2 = this.$refs.piFuWenJian.saveUploadFileList;
        this.awaitDeledtFileList3 = this.isShowHetong
          ? this.$refs.heTong.saveUploadFileList
          : [];
        this.$refs.shenBaoZiLiao.saveUploadFileList = [];
        this.$refs.piFuWenJian.saveUploadFileList = [];
        if (this.isShowHetong) this.$refs.heTong.saveUploadFileList = [];
      }
      if (!value) {
        this.$refs.shenBaoZiLiao.saveUploadFileList = this.awaitDeledtFileList1;
        this.$refs.piFuWenJian.saveUploadFileList = this.awaitDeledtFileList2;
        if (this.isShowHetong)
          this.$refs.heTong.saveUploadFileList = this.awaitDeledtFileList3;
      }
    },
    changeCheckbox1(value) {
      if (value) {
        this.awaitDeledtFileList1 = this.$refs.shenBaoZiLiao.saveUploadFileList;
        this.$refs.shenBaoZiLiao.saveUploadFileList = [];
      }
      if (!value) {
        this.$refs.shenBaoZiLiao.saveUploadFileList = this.awaitDeledtFileList1;
      }
    },
    changeCheckbox2(value) {
      if (value) {
        this.awaitDeledtFileList2 = this.$refs.piFuWenJian.saveUploadFileList;
        this.$refs.piFuWenJian.saveUploadFileList = [];
      }
      if (!value) {
        this.$refs.piFuWenJian.saveUploadFileList = this.awaitDeledtFileList2;
      }
    },
    changeCheckbox3(value) {
      if (value && this.isShowHetong) {
        this.awaitDeledtFileList3 = this.$refs.heTong.saveUploadFileList;
        this.$refs.heTong.saveUploadFileList = [];
      }
      if (!value) {
        this.$refs.heTong.saveUploadFileList = this.awaitDeledtFileList3;
      }
    },
    getInfo(projectId, stageId) {
      this.uploadForm.uploadAddress1 = `${process.env.VUE_APP_BASE_URL}/archives/establishFiles/upload/${projectId}/${stageId}/1`;
      this.uploadForm.uploadAddress2 = `${process.env.VUE_APP_BASE_URL}/archives/establishFiles/upload/${projectId}/${stageId}/2`;
      if (this.isShowHetong)
        this.uploadForm.uploadAddress3 = `${process.env.VUE_APP_BASE_URL}/archives/establishFiles/upload/${projectId}/${stageId}/3`;

      setTimeout(() => {
        this.$refs.shenBaoZiLiao.queryFile(projectId, stageId, 1);
        this.$refs.piFuWenJian.queryFile(projectId, stageId, 2);
        this.isShowHetong && this.$refs.heTong.queryFile(projectId, stageId, 3);
      }, 500);

      getUpdateInfo(projectId, stageId).then((res) => {
        // console.log(res);
        res.data.involve = res.data.involve === "true" ? true : false;
        res.data.shenbao = res.data.shenbao === "true" ? true : false;
        res.data.pifu = res.data.pifu === "true" ? true : false;
        res.data.hetong = res.data.hetong === "true" ? true : false;
        this.isSave = res.data.isSave === "true" ? true : false;
        this.editUploadForm = res.data;
      });
    },
    save() {
      this.$confirm("保存后，预计完成之间不可修改, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(() => {
          if (this.editUploadForm.involve) {
            this.awaitDeledtFileList1.forEach((item) => {
              this.$refs.shenBaoZiLiao.fileRemove(item);
            });
            this.awaitDeledtFileList2.forEach((item) => {
              this.$refs.piFuWenJian.fileRemove(item);
            });
            this.awaitDeledtFileList3.forEach((item) => {
              this.$refs.heTong.fileRemove(item);
            });
          }
          if (!this.editUploadForm.involve) {
            if (this.editUploadForm.shenbao) {
              this.awaitDeledtFileList1.forEach((item) => {
                this.$refs.shenBaoZiLiao.fileRemove(item);
              });
            }
            if (this.editUploadForm.pifu) {
              this.awaitDeledtFileList2.forEach((item) => {
                this.$refs.piFuWenJian.fileRemove(item);
              });
            }
            if (this.editUploadForm.hetong) {
              this.awaitDeledtFileList3.forEach((item) => {
                this.$refs.heTong.fileRemove(item);
              });
            }
          }
          updateDemand({
            ...this.editUploadForm,
            stageId: this.stageId,
            stageName: this.stageName,
          }).then((res) => {
            this.$message.success("保存成功！");
            this.isSave = true;
            this.awaitDeledtFileList1 = [];
            this.awaitDeledtFileList2 = [];
            this.awaitDeledtFileList3 = [];
          });
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "已取消保存！",
          });
        });
    },
    downloadAllFile() {
      // console.log(this.currentProjectId);
      this.$download.zip(
        `/archives/establishFiles/downloadAllFiles/${this.projectId}/${this.stageId}`,
        `${this.title}_${new Date().getTime()}`
      );
    },
  },
};
</script>

<style scoped>
.save-btn {
  position: absolute;
  right: 0;
}
.upload-form {
  position: relative;
  height: 60vh;
  overflow: auto !important;
  padding-right: 10px;
}
.title {
  color: #606266;
  font-weight: bold;
  /* position: absolute;
  left: 10px; */
  text-align: left;
}
</style>
